package cn.edu.zut.hkxy.controller;

import cn.edu.zut.hkxy.pojo.AjaxResult;
import cn.edu.zut.hkxy.pojo.Feature;
import cn.edu.zut.hkxy.service.FeatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/feature")
public class FeatureController {
    @Autowired
    private FeatureService featureService;

    @RequestMapping("/manage")
    public String toFeatureManage(){
        return "featureManage";
    }
    @RequestMapping("/feature-list-json")
    @ResponseBody
    public List<Feature> getFeatureList(){
        List<Feature> list=featureService.getAll();
        for (Feature feature:list){
            System.out.println(feature);
        }
        return list;
    }
    @RequestMapping("/feature-add")
    @ResponseBody
    public AjaxResult addFeature(@RequestParam MultipartFile image, Feature feature){
        AjaxResult result=new AjaxResult();
        try {
            if(featureService.addFeature(image,feature)){
                result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            }
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/feature-delete")
    @ResponseBody
    public AjaxResult deleteFeature(@RequestParam Integer id){
        AjaxResult result=new AjaxResult();
        try {
            if(featureService.deleteFeature(id)){
                result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            }
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }

    @RequestMapping("/feature-modify/{id}")
    @ResponseBody
    public AjaxResult modifyFeature(@PathVariable Integer id, Feature feature,@RequestParam MultipartFile image){
        AjaxResult result=new AjaxResult();
        try {
            feature.setId(id);
            if(featureService.modifyFeatture(feature,image)){
                result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            }
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/feature-json/{id}")
    @ResponseBody
    public Feature getFeatureById(@PathVariable Integer id){
        Feature feature=featureService.getFeatureById(id);
        return feature;
    }
}
